//
//  HLJVerificationCodeBtn.m
//  ChatDemo-UI2.0
//
//  Created by 崔志伟 on 2017/12/28.
//  Copyright © 2017年 崔志伟. All rights reserved.
//

#import "JBWVerificationCodeBtn.h"

@interface JBWVerificationCodeBtn ()
{
    dispatch_source_t _timer ;
}

@end

@implementation JBWVerificationCodeBtn
{
    UIActivityIndicatorView * activityV;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self  = [super initWithFrame:frame]) {
        [self setupSubView];

    }
    return self;
}

- (void)setupSubView {
    self.backgroundColor = [UIColor orangeColor];
    self.layer.cornerRadius = 5.0f;
    self.layer.masksToBounds = YES;
    [self setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.titleLabel.font = [UIFont systemFontOfSize:13];
    [self addTarget:self
             action:@selector(eventForClick)
   forControlEvents:UIControlEventTouchUpInside];
    [self setupInitialState];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubView];
    }
    return self;
}


-(void)initActivityView
{
    activityV  = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityV.frame=CGRectMake(2, 3, self.frame.size.height-6, self.frame.size.height-6);
    activityV.hidden=YES;
    [self addSubview:activityV];
    
}
- (void)startAnimating
{
    self.userInteractionEnabled = NO;
    [self setTitle:@"  获取中" forState:UIControlStateNormal];
    [activityV startAnimating];
    
}
- (void)stopAnimating
{
    activityV.hidden=NO;
    [activityV stopAnimating];
}
/**
 设置这个获得验证码Btn的属性
 */
- (void)setupRunningBtn
{
    self.userInteractionEnabled = NO;
    
}
- (void)setupInitialState {
    if (_timer!=nil) {
         dispatch_source_cancel(_timer);
    }
    [self setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.userInteractionEnabled = YES;
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

}

/**
 获取失败
 */
- (void)setupFailState {
    
    [self setTitle:@"重获验证码" forState:UIControlStateNormal];
    self.userInteractionEnabled = YES;
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

}
- (void)setupRunningState:(NSString *)timerSec {
    NSString *title = [NSString stringWithFormat:@"%@s重新获取",timerSec];
    [self setTitle:title forState:UIControlStateNormal];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

}

/**
 点击获取验证码的事件
 */
- (void)eventForClick {
    [self initActivityView];
    if (self.clickblock) {
        self.clickblock();
    }
}
/**
 开启定时器
 */
- (void)startTimer {
    self.userInteractionEnabled = NO;
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,DISPATCH_TIME_NOW,1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0)
        { //倒计时结束，关闭
            dispatch_source_cancel(self->_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self setupFailState];
            });
        }
        else
        {
            NSString *strTime = [NSString stringWithFormat:@"%.2d", timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self setupRunningState:strTime];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}


@end
